import Vue from 'vue';

import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        count: 1,
        search_value: '',
        car: []
    },
    mutations: {
        addToCar(state, arg) {
            let flag = false
            state.car.some(item => {
                if (item.id === arg.id) {
                    item.count += 1
                    flag = true
                    return true
                }
            })

            if (!flag) {
                state.car.push(arg)
            }
        }
    },
    getters: {
        getQuantity(state) {
            return state.car.length
        },
        getTotalCost(state) {
            let cost = 0
            state.car.forEach(item => {
                if (item.selected) {
                    cost += item.count * item.goodsMsg.price
                }
            })
            return cost * 100
        }
    },
    actions: {},
    modules: {},
})